package org.hgvc.webapp.ets.jsf.beans.admin;

import java.util.ArrayList;
import java.util.List;

import javax.faces.component.UIParameter;
import javax.faces.event.ActionEvent;
import javax.faces.model.SelectItem;

import org.hgvc.webapp.ets.constant.AdminConstant;
import org.hgvc.webapp.ets.constant.SystemConstant;
import org.hgvc.webapp.ets.spring.service.AdminService;
import org.jocsb.base.AbstractBean;
import org.jocsb.base.bo.Role;
import org.jocsb.base.bo.User;
import org.jocsb.base.exception.BaseException;
import org.jocsb.base.util.SpringUtils;


public class UserManagementBean extends AbstractBean {

	public static final String BEAN_NAME="userMgmtBean";
	private AdminService adminService;
	private List<User> userList;
	private User newUser;
	private String retypePassword;
	private List<SelectItem> rolesList;
	private String selectRoleId;
	private User selectedUser;
	private static final String LIST_USER="superAdminListUser";
	private static final String CREATE_USER="superAdminCreateUser";
	private static final String EDIT_USER="superAdminEditUser";

	private boolean catchError;
	
	public User getSelectedUser() {
		return selectedUser;
	}
	public void setSelectedUser(User selectedUser) {
		this.selectedUser = selectedUser;
	}
	public String getSelectRoleId() {
		return selectRoleId;
	}
	public void setSelectRoleId(String selectRoleId) {
		this.selectRoleId = selectRoleId;
	}
	public void setRolesList(List<SelectItem> rolesList) {
		this.rolesList = rolesList;
	}
	public User getNewUser() {
		return newUser;
	}
	public void setNewUser(User newUser) {
		this.newUser = newUser;
	}
	public String getRetypePassword() {
		return retypePassword;
	}
	public void setRetypePassword(String retypePassword) {
		this.retypePassword = retypePassword;
	}
	public List<User> getUserList() {
		try{
			userList = adminService.getUserList();
		}catch(BaseException ex){
			ex.printStackTrace();
			initErrorBean(ex);
		}
		return userList;
	}
	public void setUserList(List<User> userList) {
		this.userList = userList;
	}

	public AdminService getAdminService() {
		return adminService;
	}

	public void setAdminService(AdminService adminService) {
		this.adminService = adminService;
	}
	
	public String navUserCreationAction(){
		newUser = new User();
		selectRoleId = null;
		retypePassword = null;
		return CREATE_USER.concat("_"+getRoleName());
	}
	
	public String navListAction(){
		return LIST_USER.concat("_"+getRoleName());
	}
	
	public String createUserAction(){
		try{
			
			//validate
			if(newUser.getPassword().compareTo(retypePassword)!=0){
				throw new BaseException(SpringUtils.getMessage("error.password.not.match", new Object[]{}));
			}
			adminService.createUser(newUser, getThisUser(),selectRoleId);
			return LIST_USER.concat("_"+getRoleName());
		}catch(BaseException ex){
			ex.printStackTrace();
			initErrorBean(ex);
		}
		
		return CREATE_USER.concat("_"+getRoleName());
	}
	
	public List<SelectItem> getRolesList() {
		try{
			List<Role> rolelist = adminService.getRoleList();
			rolesList = new ArrayList<SelectItem>();
			/*for(Role role:rolelist){
				SelectItem item = new SelectItem(role.getRoleId(),role.getName());
				rolesList.add(item);
			}*/
			SelectItem adminRole = new SelectItem(SystemConstant.ADMIN_ROLE.getRoleId(),SystemConstant.ADMIN_ROLE.getName());
			rolesList.add(adminRole);
			SelectItem financeRole = new SelectItem(SystemConstant.FINANCE_ADMIN_ROLE.getRoleId(),SystemConstant.FINANCE_ADMIN_ROLE.getName());
			rolesList.add(financeRole);
			SelectItem salesAdminRole = new SelectItem(SystemConstant.SALES_ADMIN_ROLE.getRoleId(),SystemConstant.SALES_ADMIN_ROLE.getName());
			rolesList.add(salesAdminRole);
			SelectItem memberServiceRole = new SelectItem(SystemConstant.MEMBER_SERVICE_ROLE.getRoleId(),SystemConstant.MEMBER_SERVICE_ROLE.getName());
			rolesList.add(memberServiceRole);
			
		}catch(BaseException ex){
			ex.printStackTrace();
			initErrorBean(ex);
		}
		return rolesList;
	}
	
	public String activeUserList(){
		try{
			for(User usr:userList){
				if(usr.isMarkAsAction()){
					adminService.deActiveUser(usr, getThisUser(), true);
					usr.setMarkAsAction(false);
				}
			}
		}catch(BaseException ex){
			ex.printStackTrace();
			initErrorBean(ex);
		}
		
		return EDIT_USER.concat("_"+getRoleName());
	}
	
	public String deactiveUserList(){
		try{
			for(User usr:userList){
				if(usr.isMarkAsAction()){
					adminService.deActiveUser(usr, getThisUser(), false);
					usr.setMarkAsAction(false);
				}
			}
		}catch(BaseException ex){
			ex.printStackTrace();
			initErrorBean(ex);
		}
		
		return LIST_USER.concat("_"+getRoleName());
	}
	
	public void editListener(ActionEvent actionEvent){
		selectedUser = null;
		UIParameter parameter = (UIParameter)actionEvent.getComponent().findComponent("userId");
		String userId = (String)parameter.getValue();
		for(User usr:userList){
			if(usr.getUserId().equalsIgnoreCase(userId)){
				selectedUser = usr;
				break;
			}
		}
	}
	
	public String navUserEditAction(){
		retypePassword = null;
		if(selectedUser!=null){
			selectRoleId = selectedUser.getRole().getRoleId();
		}
		return EDIT_USER.concat("_"+getRoleName());
	}
	
	public String editUserAction(){
		try{
			//validate
			if(selectedUser.getPassword().compareTo(retypePassword)!=0){
				throw new BaseException(SpringUtils.getMessage("error.password.not.match", new Object[]{}));
			}
			adminService.editUser(selectedUser, getThisUser(),selectRoleId);
			return LIST_USER.concat("_"+getRoleName());
		}catch(BaseException ex){
			ex.printStackTrace();
			initErrorBean(ex);
		}
		
		return EDIT_USER.concat("_"+getRoleName());
	}
	
	public String navChangeUserPwdAction(){
		String outcome=AdminConstant.NAV_STATUS_SUCCESS.concat("_"+getRoleName());
		if (this.catchError)
			outcome=null;
		return outcome;
	}
	
	public void navChangeUserPwdActionEvent(ActionEvent event){
		this.selectedUser=getThisUser();
	}
	
	public String changeUserPwdAction(){
		String outcome=AdminConstant.NAV_STATUS_SUCCESS.concat("_"+getRoleName());
		if (this.catchError)
			outcome=null;
		return outcome;
	}

	public void changeUserPwdActionEvent(ActionEvent event){
		this.catchError=false;
		try{
			adminService.changeUserPassword(this.selectedUser);
		}catch(BaseException be){
			this.catchError=true;
			initErrorBean(be);
		}
	}
	
	public String backToUserList(){
		return LIST_USER.concat("_"+getRoleName());
	}
}
